/***********************************************************************************************************************
**                                                    Includes                                                        **
***********************************************************************************************************************/
#include "Afe.h"
#include "Twi.h"
#include "systick.h"
#include "GpioDriver.h"

/***********************************************************************************************************************
**                                           Global Variable Definitions                                              **
***********************************************************************************************************************/


/***********************************************************************************************************************
**                                            Private Macro Definitions                                               **
***********************************************************************************************************************/
//Define MTP register addr
#define MTP_SCONF1			(0X00u)
#define MTP_SCONF2			0X01u
#define MTP_OVH             0X03u    /* 0X2,3: 过充放保护电压及延时设置*/
#define MTP_OVL             0X52u

#define MTP_UVT             0X04u    /* 0x4,5:过充恢复电压设置*/
#define MTP_OVRL            0X05u

#define MTP_UV             	0X06u    /* 过放电电压设置*/

#define MTP_SCONF2          0X01u    /*0x1 放电设置*/

#define MTP_OCD1V           0X0Cu    /* 0xc电流1设置*/
#define MTP_LOV				0X0Au
#define MTP_OCD2V           0X0Du    /* 0xd电流2设置*/
#define MTP_SCV             0X0Eu    /* 短路保护设置*/
#define MTP_OCCV            0X0Fu    /* 充电过流设置 */
#define MTP_BALV			0X08u
#define MTP_SCONF           0X00u    /* 保护是否关闭mosfet 4：平衡功能*/
#define MTP_OTC             0X11u    /* 充电过温保护阈值 */
#define MTP_OTCR            0X12u    /* 充电过温释放阈值 */
#define MTP_UTC             0X13u    /* 充电低温保护阈值 */
#define MTP_UTCR            0X14u    /* 充电低温释放阈值 */
#define MTP_OTD             0X15u    /* 放电过温保护阈值 */
#define MTP_OTDR            0X16u    /* 放电过温释放阈值 */
#define MTP_UTD             0X17u    /* 放电低温保护阈值 */
#define MTP_UTDR            0X18u    /* 放电低温释放阈值 */
#define MTP_TR              0X19u    /* 内部电阻系数参考 */

#define MTP_CONF			0x40u    /* 7:电流保护控制位 2：看门狗设置 4-6：mosfet控制*/
#define MTP_BALANCEH		0x41u    /* 0x41,42:开启平衡回路*/
#define MTP_BALANCEL		0x42u
#define MTP_BSTATUS1		0x43u    /* 0x43:过充放保护状态 0：充电 1：放电 2-5：电流保护*/
#define MTP_BSTATUS2		0x44u    /* 0x44:充放电温度保护 */
#define MTP_BSTATUS3		0x45u    /* 0x45:充放电状态7：充电 6：放电 0-2：mosfet开关 */
#define MTP_TEMP1			0x46u
#define MTP_TEMP2			0x48u
#define MTP_TEMP3			0x4Au
#define MTP_CUR				0x4Cu
#define MTP_CELL1			0x4Eu
#define MTP_CELL2			0x50u
#define MTP_CELL3			0x52u
#define MTP_CELL4			0x54u
#define MTP_CELL5			0x56u
#define MTP_CELL6			0x58u
#define MTP_CELL7			0x5Au
#define MTP_CELL8			0x5Cu
#define MTP_CELL9			0x5Eu
#define MTP_CELL10			0x60u
#define MTP_CELL11			0x62u
#define MTP_CELL12			0x64u
#define MTP_CELL13			0x66u
#define MTP_CELL14			0x68u
#define MTP_CELL15			0x6Au
#define MTP_CELL16			0x6Cu
#define MTP_ADC2			  0x6Eu  // 0x4Cu 
#define MTP_BFLAG1			0x70u    /*过充保护标志位 手动清除0：充电 1：放电 2：4：5：电流*/
#define MTP_BFLAG2			0x71u    /*温度保护标志位 手动清除4：VADC转换完成 */
#define MTP_RSTSTAT			0x72u    /* 看门狗溢出时间控制 */

#define AFE_CHANNEL1		0u
#define AFE_CHANNEL2		1u

#define AFE_TWI_BUSY        2u
/***********************************************************************************************************************
**                                            Private Type Definitions                                                **
***********************************************************************************************************************/


/***********************************************************************************************************************
**                                           Private Variable Definitions                                             **
***********************************************************************************************************************/
static AFE_Info_Type AFE_Info1;
static AFE_Info_Type AFE_Info2;

/***********************************************************************************************************************
**                                           Private Function Declarations                                            **
***********************************************************************************************************************/

static uint8 AFE_GetTemperature(uint8 channel);
static uint8 AFE_GetCur(uint8 channel);
static uint8 AFE_GetVol(uint8 channel);
static uint8 AFE_GetTempTR(uint8 channel);

/**
  * @brief  get the information pack of afe
  * @param  channel 0:afe1 1:afe2
  * @retval Pointer AFE_INFO_TYPE
  */
AFE_Info_Type* Afe_GetAfeInfo(uint8 channel)
{
	AFE_Info_Type* type;
	if (AFE_CHANNEL1 == channel)
	{
		type = &AFE_Info1;
	}
    else if (AFE_CHANNEL2 == channel)
	{
		type = &AFE_Info2;
	}
	else
	{
		type = NULL_PTR;
	}
	return type;
}

/**
  * @brief  reset afe1 and afe2
  * @param  None
  * @retval 0:succes else:failed
  */
static Std_ReturnType ResetAFE( void )
{
	Std_ReturnType ret = E_NOT_OK;
	/* set reset value */
	uint8 WrBuf = 0xC0;
    /* write fae reset address */
	if ( (E_OK == Twi_Write(I2C_AFE1, AFE_ID, FAE_RSTAddr, 1u, &WrBuf,RAM))/*\
        && (E_OK == Twi_Write(I2C_AFE2,AFE_ID,FAE_RSTAddr,1,&WrBuf,RAM))*/)
	{
		ret = E_OK;
	}
	else
	{
		/*do nothing*/
	}
	return ret;
}

/**
  * @brief  init rom register 
  * @param  afe_channel:afe channel reg:register valuea:value
  * @retval 0:succes else:failed
  */
static Std_ReturnType AFE_InitRomRegValue(uint32 afe_channel,uint8 reg, uint8 value)
{
    Std_ReturnType ret;
    uint8 tempVal[2];
	/* read old value*/
    ret = TwiRead(afe_channel,AFE_ID, reg, 1+1, tempVal);
	if (E_OK == ret)
	{
		/* check the write value is equel read value */
		if(value != tempVal[0])
		{
			/* set value */
			tempVal[0] = value;
			ret = Twi_Write(afe_channel,AFE_ID, reg, 1, &tempVal[0],RAM);
			delay_1ms(50);
		}
	}
    return ret;
}

/**
  * @brief  init ram register 
  * @param  afe_channel:afe channel reg:register valuea:value
  * @retval 0:succes else:failed
  */
static Std_ReturnType AFE_InitRamRegValue(uint32 afe_channel,uint8 reg, uint8 value)
{
    Std_ReturnType ret;
    uint8 tempVal[2];
	
	/* read old value */
	ret = TwiRead(afe_channel,AFE_ID, reg, 1+1, tempVal);
	if (ret)
	{
		return ret;
	}
	/* check init value is equel to read value*/
	if(value != tempVal[0])
	{
		/* set value */
		tempVal[0] = CONF_INIT_VAL;
		ret = Twi_Write(afe_channel,AFE_ID, reg, 1, tempVal,RAM);
		if (ret)
		{
			return ret;
		}
		delay_1ms(1);
	}
    return ret;
}

/* init impletion function */
static Std_ReturnType AFE_RomInit_Impletion(uint32 afe_num)
{
	Std_ReturnType ret;
	/* enable flash mode */
	GpioDriver_SetOutputState(VPRO_AFE1_EN,TRUE);
    GpioDriver_SetOutputState(VPRO_AFE2_EN,TRUE);
    delay_1ms(15);
    /* set sconf1 register */
    ret = AFE_InitRomRegValue(afe_num,MTP_SCONF1,SCONF1_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_SCONF2,SCONF2_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
	/* set balance vol */
    ret = AFE_InitRomRegValue(afe_num,MTP_BALV,BAVL_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
	/* set low vol */
    ret = AFE_InitRomRegValue(afe_num,MTP_LOV,LOV_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_UV,AFE_UV_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_OCD1V,AFE_OCD1V_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_OCD2V,AFE_OCD2V_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_SCV,AFE_SCV_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_OCCV,AFE_OCCV_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_OTC,AFE_OTC_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_UTC,AFE_UTC_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_OTD,AFE_OTD_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
    ret = AFE_InitRomRegValue(afe_num,MTP_UTD,AFE_UTD_INIT_VAL);
	if (E_NOT_OK == ret)
	{
		goto ReturnHandler;
	}
	/*disable flash mode */
    GpioDriver_SetOutputState(VPRO_AFE1_EN,FALSE);
    GpioDriver_SetOutputState(VPRO_AFE2_EN,FALSE);
	delay_1ms(10);
	/* reset chip */
	ResetAFE();
    /* below is initnal ram register*/
	delay_1ms(30);
	return ret;
	
ReturnHandler:
    GpioDriver_SetOutputState(VPRO_AFE1_EN,FALSE);
    GpioDriver_SetOutputState(VPRO_AFE2_EN,FALSE);
	return ret;
}

static Std_ReturnType AFE_RamInit_Impletion(uint32 afe_num)
{
    return AFE_InitRamRegValue(afe_num,MTP_CONF,CONF_INIT_VAL);
}

/**
  * @brief  inital afe
  * @param  None
  * @retval 0:succes else:failed
  */
Std_ReturnType AFE_Init(void)
{
	Std_ReturnType ret = E_NOT_OK;
	/* init rom afe */
	if ((E_OK == AFE_RomInit_Impletion(I2C_AFE1))) //&& (E_OK == AFE_RomInit_Impletion(I2C_AFE2)))
	{
		ret = E_OK;
	}
    else
    {
        ret = E_NOT_OK;
    }
    /* init ram afe */
    if ((E_OK == AFE_RamInit_Impletion(I2C_AFE1)))// && (E_OK == AFE_RamInit_Impletion(I2C_AFE2)))
	{
		ret = E_OK;
	}
    else
    {
        ret = E_NOT_OK;
    }
    /* get an init val for info_pack */
	if (E_OK == ret)
	{
		delay_1ms(10);
		/* update afe1 */
		AFE_GetVol(AFE_CHANNEL1);
		//delay_1ms(10);
		AFE_GetCur(AFE_CHANNEL1);
		AFE_GetTemperature(AFE_CHANNEL1);
		AFE_GetTempTR(AFE_CHANNEL1);
		
		/* update afe2 */
		//AFE_GetVol(AFE_CHANNEL2);
		//AFE_GetCur(AFE_CHANNEL2);
		//AFE_GetTemperature(AFE_CHANNEL2);
		//AFE_GetTempTR(AFE_CHANNEL2);
	}
	return ret;
}

/**
  * @brief  update TR value
  * @param  channel 0:afe1 1:afe2
  * @retval 0:succes else:failed
  */
static Std_ReturnType AFE_GetTempTR(uint8 channel)
{
	uint8 mid_val[2];
	Std_ReturnType ret = E_NOT_OK;
	if (AFE_CHANNEL1 == channel)
	{
		ret = TwiRead(I2C_AFE1,AFE_ID, MTP_TR, 2, mid_val);
		AFE_Info1.CTR = (mid_val[0] & 0x7f);
	}
	else if (AFE_CHANNEL2 == channel)
	{
		//ret = TwiRead(I2C_AFE2,AFE_ID, MTP_TR, 2, mid_val);
		//AFE_Info2.CTR = (mid_val[0] & 0x7f);
	}
	else
	{
		/*do nothing */
	}
	return ret;
}

/**
  * @brief  update TEMP value
  * @param  channel 0:afe1 1:afe2
  * @retval 0:succes else:failed
  */
static Std_ReturnType AFE_GetTemperature(uint8 channel)
{
	uint8 ret;
	uint8 mid_val[7];
	if (AFE_CHANNEL1 == channel)
	{
		ret = TwiRead(I2C_AFE1,AFE_ID, MTP_TEMP1, 7, mid_val);
		if(E_OK == ret)
		{
			AFE_Info1.TCell[0] = (mid_val[4]<<8 | mid_val[5]);//T3
			AFE_Info1.TCell[1] = (mid_val[2]<<8 | mid_val[3]);//T2
			AFE_Info1.TCell[2] = (mid_val[0]<<8 | mid_val[1]);//T1
		}
	}
	else if (AFE_CHANNEL2 == channel)
	{
		ret = TwiRead(I2C_AFE2,AFE_ID, MTP_TEMP1, 7, mid_val);
		if(E_OK == ret)
		{
			AFE_Info2.TCell[0] = (mid_val[2]<<8 | mid_val[3]);//T2
			AFE_Info2.TCell[1] = (mid_val[4]<<8 | mid_val[5]);//T3
			AFE_Info2.TCell[2] = (mid_val[0]<<8 | mid_val[1]);//T1
		}
	}
	else
	{
		/* do nothing*/
	}
	return ret;
}

/**
  * @brief  update current value
  * @param  channel 0:afe1 1:afe2
  * @retval 0:succes else:failed
  */
static Std_ReturnType AFE_GetCur(uint8 channel)
{
	Std_ReturnType ret = E_NOT_OK;
	uint8 cur[3];
    /* get cur of channel 1 */
	if (AFE_CHANNEL1 == channel)
	{
		ret = TwiRead(I2C_AFE1,AFE_ID, MTP_ADC2, 3, cur);
		if(E_OK == ret)
		{
            /* calc the value */
			AFE_Info1.CCell = ((cur[0]<<8 | cur[1])&0xffff);
		}
	}
    /* get cur of channel 2 */
	else if (AFE_CHANNEL2 == channel)
	{
		ret = TwiRead(I2C_AFE2,AFE_ID, MTP_ADC2, 3, cur);
		if(E_OK == ret)
		{
            /* calc the value */
			AFE_Info2.CCell = ((cur[0]<<8 | cur[1])&0xffff);
		}
	}
	else
	{
		/* do nothing */
	}
	return ret;
}

/**
  * @brief  update vol value
  * @param  channel 0:afe1 1:afe2
  * @retval 0:succes else:failed
  */
static Std_ReturnType AFE_GetVol(uint8 channel)
{
	Std_ReturnType ret = E_NOT_OK;
	uint8 i;
	uint8 vol[(CELL_NUM*2)+1];
    /* get vol of channel 1 */
	if (AFE_CHANNEL1 == channel)
	{
		ret = TwiRead(I2C_AFE1,AFE_ID, MTP_CELL1, (CELL_NUM*2)+1, vol);
		if(E_OK == ret)
		{
			for(i = 0; i < CELL_NUM; i++)
			{
                /* calc the value */
				AFE_Info1.VCell[i] = (vol[i*2]<<8 | vol[i*2 + 1]);
			}
		}
	}
    /* get vol of channel 2 */
	else if (AFE_CHANNEL2 == channel)
	{
		ret = TwiRead(I2C_AFE2,AFE_ID, MTP_CELL1, (CELL_NUM*2)+1, vol);
		if(E_OK == ret)
		{
			for(i = 0; i < CELL_NUM; i++)
			{
                /* calc the value */
				AFE_Info2.VCell[i] =0;// (vol[i*2]<<8 | vol[i*2 + 1]);
			}
		}
	}
	else
	{
		/* do nothing */
	}
	return ret;
}

/**
  * @brief  read mosfet status
  * @param  ctrl:which mos
  * @retval 0:close 1:open else:failed
  */
uint8 AFE_ReadInfo(AFE_MosCtrlType ctrl)
{
    uint8 mos;
    uint8 val;
	if (E_OK == TwiRead(I2C_AFE1,AFE_ID, MTP_BSTATUS3, 1, &mos))
	{
        /* calc the value */
        if(ctrl == CHG)
        {
            val = ((mos>>1)&0x1);
        }
        else if(ctrl == DSG)
        {
            val = ((mos>>0)&0x1);
        }
        else if(ctrl == PCHG)
        {
            val = ((mos>>2)&0x1);
        }
	}
    else
    {
        val = AFE_TWI_BUSY;
    }
    return val;
}

/**
  * @brief  control mosfet
  * @param  ctrl：which one val：1:close 0:open
  * @retval 0:succes else:failed
  */
Std_ReturnType AFE_MosfetCtrl(AFE_MosCtrlType ctrl,BOOL val)
{
    uint8 mos;
    uint8 mosOld;
    uint16 twi_Count = 0x2fff;
	Std_ReturnType ret = E_NOT_OK;
	ret = TwiRead(I2C_AFE1,AFE_ID, MTP_CONF, 1, &mos);
    mosOld = mos;
	if (E_OK == ret)
	{
		if(ctrl == CHG)
		{
			mos &= ~(0x1<<CHG);
			mos |= (val<<CHG);
		}
		else if(ctrl == DSG)
		{
			mos &= ~(0x1<<DSG);
			mos |= (val<<DSG);
		}
		else if(ctrl == PCHG)
		{
			mos &= ~(0x1<<PCHG);
			mos |= (val<<PCHG);
		}
        if (mosOld != mos)
        {
            ret = Twi_Write(I2C_AFE1,AFE_ID,MTP_CONF,1,&mos,RAM);
            while (0 != twi_Count)
            {
                twi_Count--;
            }
        }
	}
	return ret;
}

/**
  * @brief  control balance
  * @param  ctrl：bit0~15 = afe1 bit16~31 = afe2
  * @retval 0:succes else:failed
  */
Std_ReturnType AFE_BalanceCtrl(uint32 ctrlCmd)
{
	Std_ReturnType ret = E_NOT_OK;
	uint8 balMaskL = 0x0Fu;
	uint8 balMaskH = 0xFFu;
	uint16 ctrlValueH = 0;
    uint16 ctrlValueL = 0;

	/* write afe1 balance value */
	ctrlValueL = (uint16)(((ctrlCmd & balMaskH) <<8) | ((ctrlCmd >> 8) & balMaskL));
	ctrlValueH = (uint16)((((ctrlCmd >> 16) & balMaskH) <<8) | ((ctrlCmd >> 24) & balMaskL));
    if (E_OK == Twi_Write(I2C_AFE1, AFE_ID, MTP_BALANCEH, 2, (uint8*)&ctrlValueL, RAM))/* && \
        E_OK == Twi_Write(I2C_AFE2, AFE_ID, MTP_BALANCEH, 2, (uint8*)&ctrlValueH, RAM))*/
    {
        ret = E_OK;
    }
	return ret;
}

/**
  * @brief  read balance status
  * @param  uint32* value
  * @retval 0:succes else:failed
  */
Std_ReturnType AFE_ReadBalance(uint32 *statusValue)
{
	Std_ReturnType ret = E_NOT_OK;
	uint8 temp[3];
    /* clear the status value */
	*statusValue = 0;
    /* read balance register */
    ret = TwiRead(I2C_AFE1,AFE_ID, MTP_BALANCEH, 3, temp);
	if (E_OK == ret)
	{
		*statusValue |= (temp[0]<<8);
		*statusValue |= temp[1];
		/*ret = TwiRead(I2C_AFE2,AFE_ID, MTP_BALANCEH, 3, temp);
		if (E_OK == ret)
		{
			*statusValue |= (temp[0]<<24);
			*statusValue |= (temp[1]<<16);
		}*/
	}
    return ret;
}
/**
  * @brief  read protect status
  * @param  channel 0:afe1 1:afe2
  * @retval 0:succes else:failed
  */
static Std_ReturnType AFE_ReadBstatus(uint8 channel)
{
	Std_ReturnType ret = E_NOT_OK;
	uint8 temp[2];
	if (AFE_CHANNEL1 == channel)
    {
		ret = TwiRead(I2C_AFE1,AFE_ID, MTP_BSTATUS1, 2, temp);
		if (E_OK == ret)
		{
			AFE_Info1.OCD1Sta = (temp[0] >> 2) & 0x1;
			AFE_Info1.OCD2Sta = (temp[0] >> 3) & 0x1;
			AFE_Info1.OCCSta = (temp[0] >> 4) & 0x1;
			AFE_Info1.SCSta	 = (temp[0] >> 5) & 0x1;
		}
	}
	else if (AFE_CHANNEL2 == channel)
	{
		ret = TwiRead(I2C_AFE2,AFE_ID, MTP_BSTATUS1, 3, temp);
		if (E_OK == ret)
		{
			AFE_Info2.OCD1Sta = (temp[0] >> 2) & 0x1;
			AFE_Info2.OCD2Sta = (temp[0] >> 3) & 0x1;
			AFE_Info2.OCCSta = (temp[0] >> 4) & 0x1;
			AFE_Info2.SCSta	 = (temp[0] >> 5) & 0x1;
		}
	}
	else
	{
		/* do nothing */
	}
	return ret;
}

/**
  * @brief  afe schdule table
  * @param  void
  * @retval void
  */
void AFE_MainFunc(void)
{
	if (AFE_Info1.Mode == AFE_Info1.Mode)//(FALSE == (AFE_Info1.Mode && AFE_Info2.Mode))
	{
		switch (AFE_Info1.step)
		{
		case VOL:
			if (E_OK == AFE_GetVol(AFE_CHANNEL1))
			{
				AFE_Info1.Alive = 0;
				//AFE_Info2.Alive = 0;
			}
			else
			{
				AFE_Info1.Alive = 1;
				//AFE_Info2.Alive = 1;
			}
			AFE_Info1.step = TEP;
			break;
		case TEP:
			AFE_GetTemperature(AFE_CHANNEL1);
			AFE_Info1.step = TR;
			break;
		case TR:
			if (E_OK == AFE_GetTempTR(AFE_CHANNEL1))
			{
				AFE_Info1.Alive = 0;
				//AFE_Info2.Alive = 0;
			}
			else
			{
				AFE_Info1.Alive = 1;
				//AFE_Info2.Alive = 1;
			}
			AFE_Info1.step = CUR;
			break;
		case CUR:
			if (E_OK == AFE_GetCur(AFE_CHANNEL1))
			{
				AFE_Info1.Alive = 0;
				//AFE_Info2.Alive = 0;
			}
			else
			{
				AFE_Info1.Alive = 1;
				//AFE_Info2.Alive = 1;
			}
		/*	AFE_Info1.step = VOL1;
			break;
		case VOL1:
			if (E_OK == AFE_GetVol(AFE_CHANNEL2))
			{
				AFE_Info1.Alive = 0;
				AFE_Info2.Alive = 0;
			}
			else
			{
				AFE_Info1.Alive = 1;
				AFE_Info2.Alive = 1;
			}
			AFE_Info1.step = TEP1;
			break;
		case TEP1:
			AFE_GetTemperature(AFE_CHANNEL2);
			AFE_Info1.step = TR1;
			break;
		case TR1:
			if (E_OK == AFE_GetTempTR(AFE_CHANNEL2))
			{
				AFE_Info1.Alive = 0;
				AFE_Info2.Alive = 0;
			}
			else
			{
				AFE_Info1.Alive = 1;
				AFE_Info2.Alive = 1;
			}
			AFE_Info1.step = CUR1;
			break;
		case CUR1:
			if (E_OK == AFE_GetCur(AFE_CHANNEL2))
			{
				AFE_Info1.Alive = 0;
				AFE_Info2.Alive = 0;
			}
			else
			{
				AFE_Info1.Alive = 1;
				AFE_Info2.Alive = 1;
			}*/
			AFE_Info1.step = STATUS;
			break;
		case STATUS:
			AFE_ReadBstatus(AFE_CHANNEL1);
			/*AFE_Info1.step = STATUS1;
			break;
		case STATUS1:
			//AFE_ReadBstatus(AFE_CHANNEL2);*/
			AFE_Info1.step = VOL;
			break;
		default:
			break;
		}
	}
	else
	{
		
	}
}

/**
  * @brief  set afe work modle
  * @param  mode：idle normal sleep
  * @retval void
  */
void AFE_SetMode(AFE_ModeType mode)
{
    uint8 flg;
    if(mode == Normal)
    {
        if (E_OK == TwiRead(I2C_AFE1,AFE_ID, MTP_CONF, 1, &flg))
        {
            AFE_Info1.Mode = mode;
        }
        if (E_OK == TwiRead(I2C_AFE2,AFE_ID, MTP_CONF, 1, &flg))
        {
            AFE_Info2.Mode = mode;
        }
    }
    else if(mode == Idle)
    {
        if (E_OK == TwiRead(I2C_AFE1,AFE_ID, MTP_CONF, 1, &flg))
        {
            flg &= ~(0x1);
            flg |= (0x1);
            Twi_Write(I2C_AFE1,AFE_ID,MTP_CONF,1,&flg,RAM);
            AFE_Info1.Mode = mode;
        }
        if (E_OK == TwiRead(I2C_AFE2,AFE_ID, MTP_CONF, 1, &flg))
        {
            flg &= ~(0x1);
            flg |= (0x1);
            Twi_Write(I2C_AFE2,AFE_ID,MTP_CONF,1,&flg,RAM);
            AFE_Info2.Mode = mode;
        }
    }
    else if(mode == Sleep)
    {
        if (E_OK == TwiRead(I2C_AFE1,AFE_ID, MTP_CONF, 1, &flg))
        {
            flg &= ~(0x1<<1);
            flg |= (0x1<<1);
            Twi_Write(I2C_AFE1,AFE_ID,MTP_CONF,1,&flg,RAM);
            AFE_Info1.Mode = mode;
        }
        if (E_OK == TwiRead(I2C_AFE2,AFE_ID, MTP_CONF, 1, &flg))
        {
            flg &= ~(0x1<<1);
            flg |= (0x1<<1);
            Twi_Write(I2C_AFE2,AFE_ID,MTP_CONF,1,&flg,RAM);
            AFE_Info2.Mode = mode;
        }
    }
}

/**
  * @brief  set AFE UV value 
  * @param  value：
  * @retval 0 ：succes else failed
  */
Std_ReturnType AFE_SetUV(uint8 val)
{
	return 0;
}
